Package deamon

Source Code of deamon.CheckStarter

package deamon;

//Importo i package necessari
import java.net.*;
import java.io.*;
import common.Config;

public class CheckStarter {

    public void start() throws IOException {

        Config myCfgMsg = new Config();
        myCfgMsg.setConfigFile("./server/cfg/server.cfg");
        String cfgLang = myCfgMsg.getProp("language");

        myCfgMsg.setConfigFile("./server/messages/messages." + cfgLang);
        System.out.println("JUNM - Java Universal NetWork Monitor - Rel 0.0.2");
        System.out.println("-------------------------------------------------");
        System.out.println("Info: Gianluca.Garofalo@gmail.com");
        System.out.println("-------------------------------------------------");
        System.out.println(myCfgMsg.getProp("006"));


        try {
            Config myCfg = new Config();
            myCfg.setConfigFile("./deamon/cfg/checkstarter.cfg");
            String cfgPort = myCfg.getProp("porta");
            ServerSocket serverSocket = new ServerSocket(Integer.parseInt(cfgPort));
            InetAddress indirizzo = serverSocket.getInetAddress();
            String server = indirizzo.getHostAddress();
            int port = serverSocket.getLocalPort();
            System.out.println("Listener Attivo: " + server + " porta: " + port);
            while (true) {
                try {
                    Socket socket = serverSocket.accept();
                    InetAddress address = socket.getInetAddress();
                    String client = address.getHostName();
                    int porta = socket.getPort();
                    DataInputStream in = new DataInputStream(socket.getInputStream());
                    BufferedReader is = new BufferedReader(new InputStreamReader(in));
                    DataOutputStream os = new DataOutputStream(socket.getOutputStream());
                    while (true) {
                        try {
                            String userInput = is.readLine();
                            if (userInput.equals("QUIT")) {
                                break;
                            }
                            String result = eseguiComando(userInput);
                            os.writeBytes(result + '\n');
                        } catch (Exception e) {
                            System.out.println("Errore: deamon.CheckStarter.start - Ciclo Interno - " + e.toString());
                        }
                    }
                    os.close();
                    is.close();
                    socket.close();
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
            System.out.println("Errore: deamon.CheckStarter.start - " + e.toString());
        }
    }

    public static void main(String[] args) throws Exception {
        try {
            CheckStarter cStarter = new CheckStarter();
            cStarter.start();
        } catch (Exception e) {
            System.out.println("Errore: deamon.CheckStarter.main - " + e.toString());
        }
    }

    public synchronized static String eseguiComando(String comando) {
        String myRes = "";
        int Ecode = 0;
        try {
            String ls_str;

            Config myCfg = new Config();
            myCfg.setConfigFile("./deamon/checks/definition.chk");
            String chkComando = myCfg.getProp(comando);

            Process ls_proc = Runtime.getRuntime().exec(chkComando);
            DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());
            BufferedReader is = new BufferedReader(new InputStreamReader(ls_in));
            while ((ls_str = is.readLine()) != null) {
                myRes = ls_str;
            }
            Ecode = ls_proc.exitValue();
        }catch(java.lang.IllegalThreadStateException jlitse){
            // exit code errato
        }catch (Exception e) {
            System.out.println("Errore: deamon.CheckStarter.eseguiComando - " + e.toString());
            myRes = "Comando Inesistente";
            Ecode = 2;
        }
        return Ecode + " " + myRes;
    }
}



TOP

Related Classes of deamon.CheckStarter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.